`include "define.v"
module IFF_IFS
(
    input   wire                                clk,
    input   wire                                rstn,
    //
    input   wire                                flush,
    input   wire                                stall,
    //
    output  reg                                 IFF_IFS_FLUSH_FLAG_o,         // 如果出现flush，则下次IF/ID寄存器接收的指令应该为NOP
    output  reg                                 IFF_IFS_STALL_FLAG_o,          //
    //
    input   wire    [63:00]                     IFF_IFS_PC_i,                         // pcReg -- pc
    input   wire    [31:00]                     IFF_IFS_INSTR_i,                      // itcm  -- instr
    output  reg     [63:00]                     IFF_IFS_PC_o,
    output  reg     [31:00]                     IFF_IFS_INSTR_o  
);
    always @(posedge clk ) begin
        if(!rstn)  begin
            IFF_IFS_PC_o                        <=  64'b0;
            IFF_IFS_INSTR_o                     <=  `BUBBLE;
            IFF_IFS_FLUSH_FLAG_o                <=  1'b0;
            IFF_IFS_STALL_FLAG_o                <=  1'b0;
        end
        else begin
            if( flush )   begin
                IFF_IFS_PC_o                        <=  64'b0;
                IFF_IFS_INSTR_o                     <=  `BUBBLE;
                IFF_IFS_FLUSH_FLAG_o                <=  1'b1;
                IFF_IFS_STALL_FLAG_o                <=  1'b0;
            end
            else if (stall) begin
                IFF_IFS_PC_o                        <= IFF_IFS_PC_o;
                IFF_IFS_INSTR_o                     <=  (IFF_IFS_STALL_FLAG_o || IFF_IFS_FLUSH_FLAG_o) ? IFF_IFS_INSTR_o : IFF_IFS_INSTR_i ;
                IFF_IFS_FLUSH_FLAG_o                <= 1'b0;
                IFF_IFS_STALL_FLAG_o                <= 1'b1;
            end
            else    begin
                IFF_IFS_PC_o                        <= IFF_IFS_PC_i;
                IFF_IFS_INSTR_o                     <= IFF_IFS_INSTR_i;
                IFF_IFS_FLUSH_FLAG_o                <= 1'b0;
                IFF_IFS_STALL_FLAG_o                <= 1'b0;
            end
        end 
    end
endmodule